namespace KPIS.GERP.Models.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class update0022 : DbMigration
    {
        public override void Up()
        {
            //RenameTable(name: "dbo.SysExecPosGroup", newName: "sys_exec_pos_group");
            //DropForeignKey("dbo.HrmMpExecPosition", "ExecPosGroupId", "dbo.SysExecPosGroup");
            //DropIndex("dbo.HrmMpExecPosition", new[] { "ExecPosGroupId" });
            //RenameColumn(table: "dbo.sys_exec_pos_group", name: "ExecPosGroupId", newName: "exec_pos_group_seq");
            //RenameColumn(table: "dbo.sys_exec_pos_group", name: "ExecPosGroupName", newName: "exec_pos_group_name");
            //RenameColumn(table: "dbo.sys_exec_pos_group", name: "CreatedBy", newName: "created_by");
            //RenameColumn(table: "dbo.sys_exec_pos_group", name: "CreatedWhen", newName: "created_when");
            //RenameColumn(table: "dbo.sys_exec_pos_group", name: "UpdatedBy", newName: "updated_by");
            //RenameColumn(table: "dbo.sys_exec_pos_group", name: "UpdatedWhen", newName: "updated_when");
            CreateTable(
                "dbo.SysApprovalTaskDtl",
                c => new
                    {
                        ApprovalTaskDtlId = c.Int(nullable: false, identity: true),
                        CreatedOnActionCount = c.Int(nullable: false),
                        IsCancel = c.Boolean(nullable: false),
                        AmountMin = c.Decimal(precision: 18, scale: 2),
                        AmountMax = c.Decimal(precision: 18, scale: 2),
                        SystemRemarks = c.String(),
                        ApprovalTaskId = c.Int(),
                        AppPathDtlId = c.Int(),
                        FromMessageId = c.Int(),
                        FromApprovalTaskDtlId = c.Int(),
                        IsApproveAble = c.Boolean(nullable: false),
                        IsForwardable = c.Boolean(nullable: false),
                        IsRejectable = c.Boolean(nullable: false),
                        IsPendable = c.Boolean(nullable: false),
                        IsSendBackable = c.Boolean(nullable: false),
                        IsLockable = c.Boolean(nullable: false),
                        IsTakebackAble = c.Boolean(nullable: false),
                        ApproverType = c.Int(nullable: false),
                        PosId = c.Int(),
                        ExecPosGroupId = c.Int(),
                        CommandByEmpId = c.Int(),
                        CommandAction = c.Int(),
                        TaskStatusBefore = c.Int(nullable: false),
                        TaskStatusAfter = c.Int(nullable: false),
                        StepBefore = c.Int(nullable: false),
                        StepAfter = c.Int(nullable: false),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.ApprovalTaskDtlId)
                .ForeignKey("dbo.hrm_ps_employees", t => t.CommandByEmpId)
                .ForeignKey("dbo.SysApprovalTask", t => t.ApprovalTaskId)
                .ForeignKey("dbo.sys_exec_pos_group", t => t.ExecPosGroupId)
                .ForeignKey("dbo.hrm_mp_positions", t => t.PosId)
                .ForeignKey("dbo.SysApprovePathDtl", t => t.AppPathDtlId)
                .Index(t => t.CommandByEmpId)
                .Index(t => t.ApprovalTaskId)
                .Index(t => t.ExecPosGroupId)
                .Index(t => t.PosId)
                .Index(t => t.AppPathDtlId);
            
            CreateTable(
                "dbo.SysApprovalTask",
                c => new
                    {
                        ApprovalTaskId = c.Int(nullable: false, identity: true),
                        Code = c.String(),
                        StepNo = c.Int(nullable: false),
                        OriginalRequestTitle = c.String(),
                        RequestAmount = c.Decimal(precision: 18, scale: 2),
                        Reasons = c.String(),
                        ActionCount = c.Int(nullable: false),
                        CurrentAppPathId = c.Int(),
                        OriginalApplyToEmpId = c.Int(nullable: false),
                        OriginalAppPrcId = c.Int(nullable: false),
                        ApprovalTaskStatus = c.Int(nullable: false),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                        RefId = c.Int(),
                        ConfirmAs = c.String(),
                        ApproveAs = c.String(),
                    })
                .PrimaryKey(t => t.ApprovalTaskId)
                .ForeignKey("dbo.SysApproveProcess", t => t.OriginalAppPrcId, cascadeDelete: true)
                .ForeignKey("dbo.hrm_ps_employees", t => t.OriginalApplyToEmpId, cascadeDelete: true)
                .ForeignKey("dbo.SysApprovePath", t => t.CurrentAppPathId)
                .Index(t => t.OriginalAppPrcId)
                .Index(t => t.OriginalApplyToEmpId)
                .Index(t => t.CurrentAppPathId);
            
            CreateTable(
                "dbo.SysApproveProcess",
                c => new
                    {
                        AppPrcId = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                        AppPrcGrpId = c.Int(nullable: false),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.AppPrcId)
                .ForeignKey("dbo.SysApproveProcessGroup", t => t.AppPrcGrpId, cascadeDelete: true)
                .Index(t => t.AppPrcGrpId);
            
            CreateTable(
                "dbo.SysApproveProcessGroup",
                c => new
                    {
                        AppPrcGrpId = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.AppPrcGrpId);
            
            CreateTable(
                "dbo.SysApprovePath",
                c => new
                    {
                        AppPathId = c.Int(nullable: false, identity: true),
                        AppPrcId = c.Int(nullable: false),
                        StepNo = c.Int(nullable: false),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.AppPathId)
                .ForeignKey("dbo.SysApproveProcess", t => t.AppPrcId, cascadeDelete: true)
                .Index(t => t.AppPrcId);
            
            CreateTable(
                "dbo.SysApprovePathDtl",
                c => new
                    {
                        AppPathDtlId = c.Int(nullable: false, identity: true),
                        ApproverType = c.Int(nullable: false),
                        IsExecPosEmployeesOnlyInSameJobSitePath = c.Boolean(nullable: false),
                        IsApproveAble = c.Boolean(nullable: false),
                        IsForwardable = c.Boolean(nullable: false),
                        IsRejectable = c.Boolean(nullable: false),
                        IsPendable = c.Boolean(nullable: false),
                        IsSendBackable = c.Boolean(nullable: false),
                        IsLockable = c.Boolean(nullable: false),
                        IsTakebackAble = c.Boolean(nullable: false),
                        AmountMin = c.Decimal(precision: 18, scale: 2),
                        AmountMax = c.Decimal(precision: 18, scale: 2),
                        AppPathId = c.Int(nullable: false),
                        AppPrcId = c.Int(nullable: false),
                        ExecPosGroupId = c.Int(),
                        PosId = c.Int(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.AppPathDtlId)
                .ForeignKey("dbo.sys_exec_pos_group", t => t.ExecPosGroupId)
                .ForeignKey("dbo.hrm_mp_positions", t => t.PosId)
                .ForeignKey("dbo.SysApproveProcess", t => t.AppPrcId, cascadeDelete: true)
                .ForeignKey("dbo.SysApprovePath", t => t.AppPathId, cascadeDelete: false)
                .Index(t => t.ExecPosGroupId)
                .Index(t => t.PosId)
                .Index(t => t.AppPrcId)
                .Index(t => t.AppPathId);
            
            //CreateTable(
            //    "dbo.sys_exec_pos_group_dtls",
            //    c => new
            //        {
            //            exec_pos_group_dtls_seq = c.Int(nullable: false, identity: true),
            //            exec_pos_group_seq = c.Int(),
            //            exec_pos_seq = c.Int(),
            //            created_by = c.Int(),
            //            created_when = c.DateTime(),
            //            updated_by = c.Int(),
            //            updated_when = c.DateTime(),
            //        })
            //    .PrimaryKey(t => t.exec_pos_group_dtls_seq)
            //    .ForeignKey("dbo.sys_exec_pos_group", t => t.exec_pos_group_seq)
            //    .ForeignKey("dbo.hrm_mp_exec_positions", t => t.exec_pos_seq)
            //    .Index(t => t.exec_pos_group_seq)
            //    .Index(t => t.exec_pos_seq);
            
            AddColumn("dbo.hrm_mp_exec_positions", "exec_pos_grp_seq", c => c.Int());
            AddColumn("dbo.SysMessage", "ApprovalTaskDtlId", c => c.Int());
            AddColumn("dbo.SysMessage", "AppPrcId", c => c.Int());
            AlterColumn("dbo.SysMessage", "FromMessageId", c => c.Int());
            AddForeignKey("dbo.hrm_mp_exec_positions", "exec_pos_grp_seq", "dbo.sys_exec_pos_group", "exec_pos_group_seq");
            AddForeignKey("dbo.SysMessage", "ApprovalTaskDtlId", "dbo.SysApprovalTaskDtl", "ApprovalTaskDtlId");
            AddForeignKey("dbo.SysMessage", "AppPrcId", "dbo.SysApproveProcess", "AppPrcId");
            CreateIndex("dbo.hrm_mp_exec_positions", "exec_pos_grp_seq");
            CreateIndex("dbo.SysMessage", "ApprovalTaskDtlId");
            CreateIndex("dbo.SysMessage", "AppPrcId");
            DropColumn("dbo.SysMessage", "ApprovalTaskID");
            DropTable("dbo.Message");
            DropTable("dbo.HrmMpExecPosition");
        }
        
        public override void Down()
        {
            CreateTable(
                "dbo.HrmMpExecPosition",
                c => new
                    {
                        ExecPosId = c.Int(nullable: false, identity: true),
                        ExecPosCode = c.String(),
                        ExecPosName = c.String(),
                        ExecPosNameShort = c.String(),
                        ExecPosNameEng = c.String(),
                        ExecPosNameEngShort = c.String(),
                        ExecPosGroupId = c.Int(),
                        Remarks = c.String(),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.ExecPosId);
            
            CreateTable(
                "dbo.Message",
                c => new
                    {
                        MessageId = c.Int(nullable: false, identity: true),
                        MessageNo = c.String(),
                        MessageDate = c.DateTime(),
                        MessageTitle = c.String(),
                        MessageDescr = c.String(),
                        FromEmpId = c.Int(),
                        ToEmpId = c.Int(),
                        FromMessageId = c.Int(),
                        ApprovalTaskId = c.Int(),
                        ApprovalTaskHistoryId = c.Int(),
                        RuleApproveId = c.Int(),
                        Remarks = c.String(),
                        MessageStatus = c.String(),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                        ActionCount = c.Int(),
                    })
                .PrimaryKey(t => t.MessageId);
            
            AddColumn("dbo.SysMessage", "ApprovalTaskID", c => c.Int());
            DropIndex("dbo.sys_exec_pos_group_dtls", new[] { "exec_pos_seq" });
            DropIndex("dbo.sys_exec_pos_group_dtls", new[] { "exec_pos_group_seq" });
            DropIndex("dbo.SysApprovePathDtl", new[] { "AppPathId" });
            DropIndex("dbo.SysApprovePathDtl", new[] { "AppPrcId" });
            DropIndex("dbo.SysApprovePathDtl", new[] { "PosId" });
            DropIndex("dbo.SysApprovePathDtl", new[] { "ExecPosGroupId" });
            DropIndex("dbo.SysApprovePath", new[] { "AppPrcId" });
            DropIndex("dbo.SysApproveProcess", new[] { "AppPrcGrpId" });
            DropIndex("dbo.SysApprovalTask", new[] { "CurrentAppPathId" });
            DropIndex("dbo.SysApprovalTask", new[] { "OriginalApplyToEmpId" });
            DropIndex("dbo.SysApprovalTask", new[] { "OriginalAppPrcId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "AppPathDtlId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "PosId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "ExecPosGroupId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "ApprovalTaskId" });
            DropIndex("dbo.SysApprovalTaskDtl", new[] { "CommandByEmpId" });
            DropIndex("dbo.SysMessage", new[] { "AppPrcId" });
            DropIndex("dbo.SysMessage", new[] { "ApprovalTaskDtlId" });
            DropIndex("dbo.hrm_mp_exec_positions", new[] { "exec_pos_grp_seq" });
            DropForeignKey("dbo.sys_exec_pos_group_dtls", "exec_pos_seq", "dbo.hrm_mp_exec_positions");
            DropForeignKey("dbo.sys_exec_pos_group_dtls", "exec_pos_group_seq", "dbo.sys_exec_pos_group");
            DropForeignKey("dbo.SysApprovePathDtl", "AppPathId", "dbo.SysApprovePath");
            DropForeignKey("dbo.SysApprovePathDtl", "AppPrcId", "dbo.SysApproveProcess");
            DropForeignKey("dbo.SysApprovePathDtl", "PosId", "dbo.hrm_mp_positions");
            DropForeignKey("dbo.SysApprovePathDtl", "ExecPosGroupId", "dbo.sys_exec_pos_group");
            DropForeignKey("dbo.SysApprovePath", "AppPrcId", "dbo.SysApproveProcess");
            DropForeignKey("dbo.SysApproveProcess", "AppPrcGrpId", "dbo.SysApproveProcessGroup");
            DropForeignKey("dbo.SysApprovalTask", "CurrentAppPathId", "dbo.SysApprovePath");
            DropForeignKey("dbo.SysApprovalTask", "OriginalApplyToEmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.SysApprovalTask", "OriginalAppPrcId", "dbo.SysApproveProcess");
            DropForeignKey("dbo.SysApprovalTaskDtl", "AppPathDtlId", "dbo.SysApprovePathDtl");
            DropForeignKey("dbo.SysApprovalTaskDtl", "PosId", "dbo.hrm_mp_positions");
            DropForeignKey("dbo.SysApprovalTaskDtl", "ExecPosGroupId", "dbo.sys_exec_pos_group");
            DropForeignKey("dbo.SysApprovalTaskDtl", "ApprovalTaskId", "dbo.SysApprovalTask");
            DropForeignKey("dbo.SysApprovalTaskDtl", "CommandByEmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.SysMessage", "AppPrcId", "dbo.SysApproveProcess");
            DropForeignKey("dbo.SysMessage", "ApprovalTaskDtlId", "dbo.SysApprovalTaskDtl");
            DropForeignKey("dbo.hrm_mp_exec_positions", "exec_pos_grp_seq", "dbo.sys_exec_pos_group");
            AlterColumn("dbo.SysMessage", "FromMessageID", c => c.Int());
            DropColumn("dbo.SysMessage", "AppPrcId");
            DropColumn("dbo.SysMessage", "ApprovalTaskDtlId");
            DropColumn("dbo.hrm_mp_exec_positions", "exec_pos_grp_seq");
            DropTable("dbo.sys_exec_pos_group_dtls");
            DropTable("dbo.SysApprovePathDtl");
            DropTable("dbo.SysApprovePath");
            DropTable("dbo.SysApproveProcessGroup");
            DropTable("dbo.SysApproveProcess");
            DropTable("dbo.SysApprovalTask");
            DropTable("dbo.SysApprovalTaskDtl");
            RenameColumn(table: "dbo.sys_exec_pos_group", name: "updated_when", newName: "UpdatedWhen");
            RenameColumn(table: "dbo.sys_exec_pos_group", name: "updated_by", newName: "UpdatedBy");
            RenameColumn(table: "dbo.sys_exec_pos_group", name: "created_when", newName: "CreatedWhen");
            RenameColumn(table: "dbo.sys_exec_pos_group", name: "created_by", newName: "CreatedBy");
            RenameColumn(table: "dbo.sys_exec_pos_group", name: "exec_pos_group_name", newName: "ExecPosGroupName");
            RenameColumn(table: "dbo.sys_exec_pos_group", name: "exec_pos_group_seq", newName: "ExecPosGroupId");
            CreateIndex("dbo.HrmMpExecPosition", "ExecPosGroupId");
            AddForeignKey("dbo.HrmMpExecPosition", "ExecPosGroupId", "dbo.SysExecPosGroup", "ExecPosGroupId");
            RenameTable(name: "dbo.sys_exec_pos_group", newName: "SysExecPosGroup");
        }
    }
}
